package com.net.xpay.common.manager.impl.admin.rbac;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.annotation.ReadTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.admin.rbac.RoleMapper;
import com.net.xpay.common.domain.admin.rbac.Role;
import com.net.xpay.common.manager.admin.rbac.RoleManager;
import com.net.xpay.common.model.admin.rbac.search.RoleSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
@ReadTransactional
public class RoleManagerImpl extends AbstractManager<Role> implements RoleManager{

    @Autowired
    private RoleMapper mapper;


    @Override
    protected BasicMapper<Long, Role> getMapper() {
        return mapper;
    }

    @Override
    public List<Role> search(RoleSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(RoleSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public Role findByName(String name) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
        Role condition = new Role();
        condition.setName(name);
        return getByCondition(condition);
    }

    @Override
    public List<Role> listAll() {
        Role condition = new Role();
        return listByCondition(condition, new ArrayList<>());
    }
}
